<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>

<%@ page import="com.broadvision.emarketing.criteriabuilder.AttributeTreeNode" %>
<%@ page import="com.broadvision.emarketing.criteriabuilder.ExpandableTreeNode" %>
<%@ page import="com.broadvision.emarketing.criteriabuilder.Tree" %>
<%@ page import="com.broadvision.emarketing.action.Constants" %>

<%!
  private static final String INDENT = "&nbsp;&nbsp;&nbsp;&nbsp;";
%>

<%
  Tree tree = (Tree)session.getAttribute(Constants.AttributeBrowse.ATTRIBUTE_BROWSE_TREE);
  pageContext.setAttribute("tree", tree);

  final String clickedKey = request.getParameter("key");
  pageContext.setAttribute("clickedKey", clickedKey);

  List nodeList = tree.getPreorderNodeList(true, true);
  pageContext.setAttribute("nodeList", nodeList);

%>
<c:set var="collapseImage">
  <html:img bundle="em" page="/mc/images/icons/icon_collapse.gif" width="15" height="15" border="0" titleKey="em.cb.attributeBrowse.image.altKey.collapse" altKey="em.cb.attributeBrowse.image.altKey.collapse" />
</c:set>
<c:set var="expandImage">
  <html:img bundle="em" page="/mc/images/icons/icon_expand.gif" width="15" height="15" border="0" titleKey="em.cb.attributeBrowse.image.altKey.expand" altKey="em.cb.attributeBrowse.image.altKey.expand" />
</c:set>
<c:set var="currentImage">
  <html:img page="/mc/images/icons/icon_current.gif" width="8"
      height="9" border="0" bundle="em" altKey="em.cb.attributeBrowse.image.altKey.selected"
      hspace="2" align="absbottom"/>
</c:set>
<SCRIPT language='Javascript'>
  function onOK(){
    <c:choose>
      <c:when test="${empty tree.selectedNode}">
         alert("<bean:message bundle="em" key="em.cb.attributeBrowse.alert.chooseAttribute"/>");
         return false;
      </c:when>
      <c:otherwise>
        <c:url var="switchUrl" value="/em/cb/attributeSwitch.do">
          <c:param name="key" value="${clickedKey}" />
          <c:param name="actType" value="create" />
        </c:url>
        window.opener.location = '<c:out value="${switchUrl}" escapeXml="false" />';
        window.opener.focus();
        window.close();
      </c:otherwise>
    </c:choose>
  }
</SCRIPT>
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageButtonWrap" align="right">
        <input type="button" name="CancelBtn" class="pageButton"
          onclick="javascript:window.close()"
          value="<bean:message bundle="em" key="button.cancel"/>" >
        <input type="button" name="okBtn" class="pageButton"
          value="<bean:message bundle="em" key="button.ok"/>"
          onclick="javascript:onOK()">            
      </div>

      <div class="pageSubtitle">
        <span class="left">
          <bean:message bundle="em" key="em.cb.attributeBrowse.pageSubtitle"/>
        </span>
      </div>
      <div class="pageInstructions">
        <bean:message bundle="em" key="em.cb.attributeBrowse.instruction"/>
      </div>
      <table class="dataTable" border="0" cellspacing="1" cellpadding="2" width="100%">

        <%
          int startIndex = 0;
          if ("/".equals(((AttributeTreeNode)nodeList.get(0)).getFriendlyDescription())) {
            //don't show "/" as the root node.
            startIndex ++;
          }
          pageContext.setAttribute("startIndex", new Integer(startIndex));
        %>
        <c:forEach items="${nodeList}" begin="${startIndex}" var="oneNode">
          <c:set var="rowStyle">
            <c:choose>
              <c:when test="${(oneNode.level + 1) % 2 == 0}">
                listEven
              </c:when>
              <c:otherwise>
                listOdd
              </c:otherwise>
            </c:choose>
          </c:set>
          <tr class="<c:out value="${rowStyle}" />" >
            <td>
              <c:if test="${oneNode.level - tree.displayRootNode.level - 1 > 0}">
                <c:forEach begin="0" end="${oneNode.level - tree.displayRootNode.level - 2}">
                  <%=INDENT%>
                </c:forEach>
              </c:if>
              <c:choose>
                <c:when test="${not oneNode.allowsChildren}">
                  <%-- This is a leaf attribute that cannot have children. --%>
                  <%-- not a branch node, so we need indent one more time for branch's collapse/expand icon. --%>
                  <%=INDENT%>
                  <c:url var="leafNodeUrl" value="/em/cb/attributeChoose.do">
                    <c:param name="key" value="${clickedKey}" />
                    <c:param name="attrKey" value="${oneNode.id}" />
                  </c:url>
                  <a href='<c:out value="${leafNodeUrl}" />'>
                    <c:out value="${oneNode.friendlyDescription}" />
                  </a>
                  <c:if test="${(not empty tree.selectedNode) && (tree.selectedNode.id == oneNode.id)}">
                    <c:out value="${currentImage}" escapeXml="false" />
                  </c:if>
                </c:when>
                <c:otherwise>
                  <%-- This is a branch attribute. --%>
                  <c:url var="branchNodeUrl" value="/em/cb/attributeBranchToggle.do">
                    <c:param name="key" value="${clickedKey}" />
                    <c:param name="attrKey" value="${oneNode.id}" />
                  </c:url>
                  <c:choose>
                    <c:when test="${oneNode.expanded}"> 
                      <%-- This branch is expanded already. --%>
                      <a href='<c:out value="${branchNodeUrl}" />'><c:out value="${collapseImage}" escapeXml="false"/>&nbsp;<c:out value="${oneNode.friendlyDescription}" /></a>
                    </c:when>
                    <c:otherwise>
                      <a href='<c:out value="${branchNodeUrl}" />'><c:out value="${expandImage}" escapeXml="false"/>&nbsp;<c:out value="${oneNode.friendlyDescription}" /></a>
                    </c:otherwise>
                  </c:choose> <%-- end of expanded choose. --%>
                </c:otherwise>
              </c:choose>
            </td>
          </tr>
        </c:forEach>
      </table>
      <div class="pageButtonWrap" align="right">
        <input type="button" name="CancelBtn" class="pageButton"
          onclick="javascript:window.close()"
          value="<bean:message bundle="em" key="button.cancel"/>" >
        <input type="button" name="okBtn" class="pageButton"
          value="<bean:message bundle="em" key="button.ok"/>"
          onclick="javascript:onOK()">            
      </div>
    </td>
  </tr>
</table>